<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <el-button size="small" type="primary" style="margin-bottom:5px"  @click="handleAdd()">新增</el-button>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="adminName" label="管理员名称" sortable width="160">
            </el-table-column>


            <el-table-column   label="状态"  >
             <template  scope="scope">
               {{scope.row.available==0?"禁用":"可用"}}
                </template>
            </el-table-column>
            
            <el-table-column label="操作" width="180">
                <template scope="scope">

                    <el-button v-if="scope.row.available==1" size="small" type="danger"
                               @click="disabled(scope.$index, scope.row)">禁用</el-button>
                    <el-button v-if="scope.row.available==0" size="small" type="success"
                               @click="enabled(scope.$index, scope.row)">启用</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                    layout="total,prev, pager, next"
                    :current-page.sync="currentPage"
                    :total="total">
            </el-pagination>
        </div>

        <!--预设区-->
        <div>
              <el-dialog title="新增管理员" v-model="formVisible">
                <el-form :model="admin">


                    <el-form-item  label="管理员账号">
                        <el-input v-model="admin.adminName" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item  label="管理员密码">
                        <el-input v-model="admin.adminPassWord" auto-complete="off"></el-input>
                    </el-form-item>




                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="formVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add()">确 定</el-button>
                </div>
            </el-dialog>

        </div>
    </div>
</template>

<script>
    import { Loading } from 'element-ui';
    export default {
        data() {
            return {
                admin:{

                    adminName:"",
                    adminPassWord:"",
                    available:true,
                },
                total:0,
                currentPage:1,
                tableData: [],
                formVisible:false
            }
        },
        methods: {


            handleAdd(index,row){

               
                this.action="add";
                this.admin={
                    adminName:"",
                    adminPassWord:"",
                    available:true,
                }
                 this.formVisible=true;

            },

            enabled(index, row) {
                let self = this;
                let params = {
                    adminId : row.adminId
                }
                this.$confirm('是否启用?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    self.$req.put(self.$api("Admin")+"enabled/"+params.adminId,self,function(res){

                        self.$message({
                            type: 'success',
                            message: '启用成功!'
                        });
                        self.getData();
                    },params)
                });


            },
            disabled(index, row) {
                 let self = this;
                let params = {
                    adminId : row.adminId
                }
                 this.$confirm('是否禁用?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(() => {
                        self.$req.put(self.$api("Admin")+"disabled/"+params.adminId,self,function(res){

                            self.$message({
                                type: 'success',
                                message: '禁用成功!'
                            });
                            self.getData();
                        },params)
                    });
                

            },
            getData(){
                let self = this;
                let params = {
                    page:self.currentPage
                };
                 self.$req.pget(self.$api("Admin")+"adminUserList",self,function(res){
                    self.tableData = res.data.data.content;

                    self.total = res.data.data.totalElements;

                },params,true)
            },

             add(){
                let self = this;
                self.formVisible = false;
                let data = self.admin;

                self.$req.post(self.$api("Admin")+"addAdminUser",self,function(res){
                    self.getData();
                },data,true)
            }
           



        },
        mounted:function(){
            this.getData();
        }
    }
</script>